# Create your models here.
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver

STATUS_ACTIVE = 1
STATUS_DELETED = 0


class Lab(models.Model):
    name = models.CharField(max_length=128, null=True, verbose_name='名称')
    pic = models.FileField(upload_to='lab/', verbose_name='实验室介绍图')
    address = models.CharField(max_length=255, null=True, verbose_name='地址')
    desc = models.CharField(max_length=255, null=True, verbose_name='实验室介绍')
    status = models.IntegerField(default=STATUS_ACTIVE, verbose_name='实验室激活')

    class Meta:
        db_table = 'lab'
        verbose_name = "实验室"  # 设置模型在Django Admin中显示的名称
        verbose_name_plural = "实验室"  # 设置模型的复数


class Device(models.Model):
    lab_id = models.IntegerField(verbose_name='关联实验室')
    name = models.CharField(max_length=128, null=True, verbose_name='设备名称')
    pic = models.FileField(upload_to='device/', verbose_name='设备介绍图')
    model = models.CharField(max_length=128, verbose_name='设备型号')
    desc = models.CharField(max_length=255, null=True, verbose_name='设备介绍')
    user_guide = models.CharField(max_length=255, null=True, verbose_name='使用说明')
    status = models.IntegerField(default=1, verbose_name='设备状态')  # 0 维修 1 空闲 2 已约满
    attach_desc = models.CharField(max_length=255, verbose_name='配套附件文字说明')
    attach_pic = models.FileField(verbose_name='附件介绍图')
    reserve_start_time = models.DateTimeField(blank=True, null=True)
    reserve_end_time = models.DateTimeField(blank=True, null=True)
    maintain_date = models.DateField(null=True, verbose_name='设备维护时间')
    maintain_type = models.IntegerField(default=0, verbose_name='设备维护时间')

    class Meta:
        db_table = 'device'
        verbose_name = "设备"  # 设置模型在Django Admin中显示的名称
        verbose_name_plural = "设备"  # 设置模型的复数

class DeviceParams(models.Model):
    device_id = models.IntegerField(null=False)
    param_name = models.CharField(max_length=128, verbose_name='参数名称')
    param_value = models.CharField(max_length=128, verbose_name='参数值')

    class Meta:
        db_table = 'device_params'
        verbose_name = "设备参数"  # 设置模型在Django Admin中显示的名称
        verbose_name_plural = "设备参数"  # 设置模型的复数


class File(models.Model):
    related_id = models.IntegerField(null=True, verbose_name='图片文件关联编号')
    file = models.FileField(upload_to='uploads/', verbose_name='设备介绍图')
    typ = models.CharField(null=True, max_length=128, verbose_name='文件类型')

    class Meta:
        db_table = 'file'
        verbose_name = "文件"
        verbose_name_plural = "文件"


class Material(models.Model):
    name = models.CharField(max_length=128, verbose_name='耗材名称')
    unit = models.CharField(max_length=32, verbose_name="耗材单位")

    class Meta:
        db_table = 'material'
        verbose_name = "耗材"
        verbose_name_plural = "耗材"


class Appointment(models.Model):
    order_number = models.CharField(max_length=64, null=True, verbose_name='预约单号')
    device_id = models.IntegerField(null=False)
    appointment_date = models.DateField(null=True, verbose_name='预约时间')
    occupied_time_slot = models.CharField(max_length=32, null=True, verbose_name='预约时间01表示')
    desc = models.CharField(max_length=255, null=True, verbose_name='用途描述')
    created_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    status = models.IntegerField(default=0, verbose_name='预约状态') # 0 待审批=pending 1 已通过=pass 2已驳回=reject 3已取消=cancel
    user_id = models.IntegerField(null=False, verbose_name='预约人')
    appointment_pic_id = models.IntegerField(null=True, verbose_name='预约图纸及文件')
    reason = models.CharField(max_length=256, null=True, verbose_name='原因')

    def save(self, *args, **kwargs):
        # 在保存前进行预创建操作
        super(Appointment, self).save(*args, **kwargs)

    class Meta:
        db_table = 'appointment'
        verbose_name = "预约表"
        verbose_name_plural = "预约表"


@receiver(pre_save, sender=Appointment)
def pre_create_or_update(sender, **kwargs):
    instance = kwargs['instance']
    if not instance.pk:  # 如果是创建新实例
        # 在这里进行预创建操作
        # 例如：设置默认值，分配权限等
        pass
    else:
        # 如果是更新现有实例，可以在这里进行预更新操作
        pass


class AppointmentMaterial(models.Model):
    appointment_id = models.IntegerField(null=False)
    material_id = models.IntegerField(null=False)
    number = models.IntegerField(null=False)

    class Meta:
        db_table = 'appointment_material'
        verbose_name = "预约耗材管理"
        verbose_name_plural = "预约耗材管理"
